<template>
  <!-- 代码测试 -->
  <div>
    <!-- 折叠栏，自定义表格控件，自定义布局控件 -->
    <!-- <toolbartop
      :attributeOrder="attributeOrder"
      :dataAttribute="dataAttribute"
      :switchForLa="switchForLa"
      @FTableFunction="FTableFunction"
    ></toolbartop>-->

    <!-- 搜索框-->
    <div style="padding: 25px 20px;">
      <searchComponents :searchData="searchData" @searchCallBack="searchCallBack"></searchComponents>
      <!-- <div class="right_row"> -->
      <el-button type="primary" size="mini" @click="sunS_F">搜索</el-button>
      <!--添加和批量删除组件-->
      <buttonhurdle class="buttonhurdle" :diaDataAdd="searchData" @toAdd="toAdd" @toDelete="toDelete"></buttonhurdle>
      <!-- </div> -->
    </div>

    <div class="out">
      <!--      <div v-show="(switchForLa.draggable||switchForLa.resizable)">{{testLayout[0].i}}</div>-->
      <!--数据列表-->
      <!-- <div>{{dataAttribute+'shuju '}}</div>          -->
      <el-table ref="multipleTable" :data="dataAttribute" v-loading.body="false" border fit highlight-current-row size="mini" :cell-style="{padding:'5px 0'}" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" :show-overflow-tooltip="true" width="55"></el-table-column>
        <el-table-column align="center" v-for="item in TableDataModel" :key="item.id" :label="item.CN" :show-overflow-tooltip="true">
          <template slot-scope="scope">{{scope.row[item.EN]}}</template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="reight">
          <template slot-scope="scope">
            <!-- 查看按钮 -->
            <el-button style="color:#0B9AC3;border:none;" icon="el-icon-view" circle @click="makeExaId(scope.row.id)"></el-button>
            <!-- 修改组件 -->
            <modificationi class="inline_block" :attributeOrder="searchData" :modData="scope.row" @modifyData="modifyData"></modificationi>
            <!-- 删除按钮 -->
            <el-button style="color:#C90010;border:none;" icon="el-icon-delete" circle @click="deleteList(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="pagetotal"></el-pagination>
    </div>

    <!-- 查看组件 -->
    <examine :DialogVisibleMod="DialogVisibleExamine" :modData="examineData" :attributeOrder="searchData" @examineDialog="examineDialog"></examine>
  </div>
</template>
<script>
//引入网格布局
import VueGridLayout from 'vue-grid-layout'
//引入Toolbar_top组件
import toolbartop from '@/views/components/FLPage/toolbartop'
//引入按钮栏
import buttonhurdle from '@/views/components/FLPage/buttonhurdle'
//引入修改组件
import modificationi from '@/views/components/FLPage/modificationi'
//引入查看组件
import examine from '@/views/components/FLPage/examine'
//引入搜索组件
import searchComponents from '@/views/components/FLPage/searchComponents'
import importXlsx from '@/views/components/FLPage/importXlsx'
import {
  addFlCustomerInfo, //增加
  deleteFlCustomerInfo, //删除
  searchFlCustomerInfo, //分页查询
  modifyFlCustomerInfo, //修改
  getFlCustomerInfo, //根据ID获取
} from '@/api/admin/flCustomerInfo/index'
export default {
  data() {
    return {
      //搜索框
      suoS: {},
      //瀑布数据
      //   搜索组件的数据
      searchData: [
        {
          EN: 'id',
          CN: '客户编号',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^.{3,20}$',
          err: '·长度为3-20!',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: true,
          modificationSign: true,
        },
        {
          EN: 'createName',
          CN: '客户名称',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^.{3,20}$',
          err: '·长度为3-20!',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'customerTel',
          CN: '电话号码',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^.{3,20}$',
          err: '·长度为3-20!',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'createTime',
          CN: '更新时间',
          value: '',
          type: 'date',
          option: [],
          typeFrom: 'string',
          Exp: '^.{3,20}$',
          err: '·长度为3-20!',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
      ],
      //添加弹窗的数据模型
      diaDataAdd: {
        yyqName: '',
        pqName: '',
        pqNum: '',
        BH: '',
      },
      //删除----Start----------------------------------------
      deleteDataList: [],
      //删除----End----------------------------------------

      //查看----Start----------------------------------------
      //查看的弹窗控制器
      DialogVisibleExamine: false,
      //查看的数据
      examineData: {},
      //查看----End----------------------------------------

      //表格数据
      //后端传来的数据
      dataAttribute: [],
      //表格选中
      multipleSelection: [],
      //分页
      currentPage: 1, //当前页
      pagesize: 10, //每页条数
      pagetotal: 0, //总条数
    }
  },
  computed: {
    //解决v-for v-if在同一标签警告问题
    TableDataModel() {
      let new_DataModel = JSON.parse(JSON.stringify(this.searchData))
      for (let i = 0; i < new_DataModel.length; i++) {
        if (!new_DataModel[i].tableSign) {
          console.log('-------------计算属性-----------')
          new_DataModel.splice(i, 1)
        }
      }
      return new_DataModel
    },
  },
  created() {
    this.forData(this.currentPage, this.pagesize, this.suoS)
  },
  methods: {
    //搜索
    sunS_F() {
      this.currentPage = 1
      this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
    },
    //查询组件回调
    searchCallBack(newData) {
      console.log(newData)
      //组装数据（newData是个数组）

      let obj = {}
      for (let i = 0; i < newData.length; i++) {
        obj[newData[i].EN] = newData[i].value
      }
      console.log(obj)
      this.suoS = obj
    },
    //渲染数据初始
    forData(page, size, searchData) {
      searchFlCustomerInfo(page, size, searchData).then((response) => {
        this.pagetotal = response.data.total
        this.dataAttribute = response.data.list
      })
    },

    //添加用户--新增表单组件传来的参数
    toAdd(addData) {
      console.log(addData)
      let obj = {}
      for (let i = 0; i < addData.length; i++) {
        obj[addData[i].EN] = addData[i].value
      }
      console.log(obj)
      addFlCustomerInfo(obj).then((response) => {
        this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
      })
    },

    //按钮栏传递了删除命令
    toDelete(orderstring) {
      this.$confirm('确认删除？')
        .then((_) => {
          let idArr = []
          for (let i = 0; i < this.deleteDataList.length; i++) {
            idArr.push(this.deleteDataList[i].id)
          }
          if (idArr.length == 0) {
            alert('请选择要删除的数据！')
            return
          }
          deleteFlCustomerInfo(idArr)
            .then((response) => {
              this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
            })
            .catch((error) => {
              alert('删除失败！')
            })
        })
        .catch((_) => {})
    },

    //单项删除
    deleteList(id) {
      console.log(id)
      let arrId = [id]
      this.$confirm('确认删除？')
        .then((_) => {
          deleteFlCustomerInfo(arrId).then((response) => {
            this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
          })
          done()
        })
        .catch((_) => {})
    },

    //查看
    makeExaId(id) {
      getFlCustomerInfo(id)
        .then((response) => {
          let copyDialog = true
          this.DialogVisibleExamine = copyDialog
          // console.log(response)
          this.examineData = response.data
        })
        .catch((error) => {
          alert('获取失败')
        })
    },
    //查看关闭弹窗
    examineDialog(dialogVisibleMod) {
      this.DialogVisibleExamine = false
    },

    //修改
    modifyData(value, modData) {
      console.log(value, modData)
      delete modData.id // 去掉id
      if (value === true) {
        modifyFlCustomerInfo(modData.id, modData)
          .then((response) => {
            console.log(response)
            this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
          })
          .catch((error) => {
            alert('修改失败')
            this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
          })
      } else {
        this.forData(this.currentPage, this.pagesize, this.suoS) //刷新列表
      }
    },

    //表格选中事件
    handleSelectionChange(val) {
      this.multipleSelection = val
      //传递删除的参数
      this.deleteDataList = val
    },

    //分页
    handleSizeChange(val) {
      console.log('每页 ' + val + ' 条')
      this.forData(this.currentPage, val, this.suoS)
    },
    handleCurrentChange(val) {
      console.log('当前页: ' + val)
      this.forData(val, this.pagesize, this.suoS)
    },

    //工具栏组件传来的参数
    // FTableFunction(attributeOrderCloneTwiceL) {
    //   this.attributeOrder = attributeOrderCloneTwiceL;
    // },
  },
  components: {
    GridLayout: VueGridLayout.GridLayout,
    GridItem: VueGridLayout.GridItem,
    toolbartop,
    buttonhurdle,
    modificationi,
    examine,
    searchComponents,
    importXlsx,
  },
}
</script>
<style scoped src="@/styles/allCss.css"></style>
